package cn.tedu.erp.project.service.impl;

import cn.tedu.erp.project.ex.ServiceException;
import cn.tedu.erp.project.mapper.FinancePayrollMapper;
import cn.tedu.erp.project.pojo.dto.FinancePayrollAddNewDTO;
import cn.tedu.erp.project.pojo.dto.FinancePayrollUpdateDTO;
import cn.tedu.erp.project.pojo.dto.FinancePayrollAddNewDTO;
import cn.tedu.erp.project.pojo.dto.FinancePayrollUpdateDTO;
import cn.tedu.erp.project.pojo.entity.FinancePayroll;
import cn.tedu.erp.project.pojo.vo.FinancePayrollListVO;
import cn.tedu.erp.project.pojo.vo.FinancePayrollStandardVO;
import cn.tedu.erp.project.pojo.vo.FinancePayrollListVO;
import cn.tedu.erp.project.pojo.vo.FinancePayrollStandardVO;
import cn.tedu.erp.project.service.IFinancePayrollService;
import cn.tedu.erp.project.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class FinancePayrollServiceImpl implements IFinancePayrollService {
    @Autowired
    private FinancePayrollMapper financePayrollMapper;

    public FinancePayrollServiceImpl() {
        log.debug("创建业务类对象：FinancePayrollServiceImpl");
    }

    @Override
    public void addNew(FinancePayrollAddNewDTO financePayrollAddNewDTO) {
        log.debug("开始处理【添加工资核算表】的业务，参数：{}", financePayrollAddNewDTO);
        // 创建FinancePayroll对象
        FinancePayroll financePayroll = new FinancePayroll();
        // 调用BeanUtils.copyProperties(源,目标)将参数对象中的属性复制到FinancePayroll对象中
        BeanUtils.copyProperties(financePayrollAddNewDTO, financePayroll);
        // 调用Mapper对象的insert()执行插入工资核算表数据
        log.debug("即将执行插入数据，参数：{}", financePayroll);
        financePayrollMapper.insert(financePayroll);
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【根据ID删除工资核算表】的业务，参数：{}", id);
        // 调用Mapper对象的getStandardById()执行查询
        FinancePayrollStandardVO financePayroll = financePayrollMapper.getStandardById(id);
        log.debug("根据ID={}检查工资核算表数据是否存在，查询结果：{}", id, financePayroll);
        // 判断查询结果是否为null
        if (financePayroll == null) {
            // 是：数据不存在，抛出异常
            String message = "删除工资核算表失败，尝试删除的工资核算表数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        // 调用Mapper对象的deleteById()方法执行删除
        log.debug("即将执行删除，参数：{}", id);
        financePayrollMapper.deleteById(id);
    }

    @Override
    public void updateInfoById(Long id, FinancePayrollUpdateDTO financePayrollUpdateDTO) {
        log.debug("开始处理【修改工资核算表详情】的业务，ID：{}，新数据：{}", id, financePayrollUpdateDTO);
        // 调用Mapper对象的getStandardById()执行查询
        FinancePayrollStandardVO queryResult = financePayrollMapper.getStandardById(id);
        // 判断查询结果是否为null
        if (queryResult == null) {
            // 是：抛出异常
            String message = "修改工资核算表详情失败，尝试修改的工资核算表数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        // 创建FinancePayroll对象
        FinancePayroll financePayroll = new FinancePayroll();
        // 复制属性，设置ID
        BeanUtils.copyProperties(financePayrollUpdateDTO, financePayroll);
        financePayroll.setId(id);
        // 调用Mapper对象的update()方法执行修改
        financePayrollMapper.update(financePayroll);
    }

    @Override
    public FinancePayrollStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询工资核算表详情】的业务，参数：{}", id);
        // FinancePayrollStandardVO queryResult = financePayrollMapper.getStandardById(id);
        FinancePayrollStandardVO queryResult = financePayrollMapper.getStandardById(id);
        if (queryResult == null) {
            // 是：抛出异常
            String message = "查询工资核算表详情失败，工资核算表数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return queryResult;
    }

    @Override
    public List<FinancePayrollListVO> list() {
        log.debug("开始处理【查询工资核算表列表】的业务，无参数");
        // List<FinancePayrollListVO> list = financePayrollMapper.list();
        List<FinancePayrollListVO> list = financePayrollMapper.list();
        return list;
    }

    @Override
    public List<FinancePayrollListVO> listByEmpId(Long employeeId) {
        log.debug("开始处理【根据属性模板查询属性列表】的业务，参数：{}", employeeId);
        return financePayrollMapper.listByEmpId(employeeId);
    }
}
